package com.itic.hpp.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.appbase.framework.utils.WXconfigTypeVo;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.user.scavenger.persistence.model.HppUserScavenger;
import com.itic.system.user.scavenger.service.HppUserScavengerService;
import com.itic.system.vo.PersonSetInVo;
import com.itic.system.user.user.persistence.model.HppUser;
import com.itic.system.user.user.service.HppUserService;
import com.itic.system.wx.persistence.model.Hppwx;
import com.itic.system.wx.service.HppwxService;

@Controller
@RequestMapping(value = "/hpp/scavenger")
public class UserScavengerInterface extends BaseMobile {

	@Autowired
	protected HppUserScavengerService hppUserScavengerService;
	@Autowired
	protected HppUserService hppUserService;
	@Autowired
	protected HppwxService hppwxService;
	

	private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "updateTime", "orgCode" };

	/**
	 * 添加扫码员
	 * 
	 * @param code
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(String code, String userId, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		String result = ResponseCode.SERVER_ERROR.getCode();
		try {
			Hppwx hppwx = hppwxService.getHppwxByType(WXconfigTypeVo.kf.getType());
			if(hppwx != null){
				if (StringHelper.isNotBlank(code) && StringHelper.isNotBlank(userId)) {
					HppUser hppUser = hppUserService.get(userId);
					if (hppUser != null) {
						result = hppUserScavengerService.addScavenger(code, userId,hppwx.getAppId(),hppwx.getAppSecret());
						if (ResponseCode.SUCCESS.getCode().equals(result)) {
							errorCode = SUCCESS_ERROR_CODE;
							errorMsg = SUCCESS_ERROR_MSG;
						}
					} else {
						errorCode = USER_ERROR_CODE;
						errorMsg = USER_ERROR_MSG;
					}
				}
			}else{
				errorCode = WX_CONFIG_ERROR_CODE;
				errorMsg = WX_CONFIG_ERROR_MSG;
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserScavengerInterface", "add", e.toString(), "添加扫码员", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 取消扫码员
	 * @param id
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, Object> del(String id, String userId, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		String result = ResponseCode.SERVER_ERROR.getCode();
		try {
			if(StringHelper.isNotBlank(id) && StringHelper.isNotBlank(userId)){
				HppUser hppUser = hppUserService.get(userId);
				if (hppUser != null) {
					Boolean isOwn = hppUserScavengerService.isOwn(userId, userId);
					if(isOwn){
						result = hppUserScavengerService.del(userId, userId);
						if (ResponseCode.SUCCESS.getCode().equals(result)) {
							errorCode = SUCCESS_ERROR_CODE;
							errorMsg = SUCCESS_ERROR_MSG;
						}
					}else{
						errorCode = USER_AUTH_ERROR_CODE;
						errorMsg = USER_AUTH_ERROR_MSG;
					}
				} else {
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserScavengerInterface", "del", e.toString(), "取消扫码员", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 根据用户Id获取扫码员列表
	 * 
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(String userId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if (StringUtils.isNotBlank(userId)) {
				HppUser hppUser = hppUserService.get(userId);
				if (hppUser != null) {
					List<HppUserScavenger> list = hppUserScavengerService.getlist(userId);
					resultMap.put(DATA, ValueHelper.tranList2MapList(list, ignoreProperties));
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				} else {
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserScavengerInterface", "list", e.toString(), "扫码员列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
}
